package com.cbgolf.oa.views;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.MyBaseAdapter;
import com.cbgolf.oa.base.BaseView;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.presenter.IPresenterImp;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.EnumUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lq on 2018/1/27.
 */

public class FairwayView extends BaseView {

  //top
  @ViewInject(R.id.topBack_ll)
  private View topBack;
  @ViewInject(R.id.title_tv)
  private TextView topTitle;
  //
  @ViewInject(R.id.a_park_present_num_tv)
  private TextView playingNumTv;
  @ViewInject(R.id.a_park_present_compare_tv)
  private TextView playingRatioTv;
  @ViewInject(R.id.a_park_no_checkin_num_tv)
  private TextView noCheckInNumTv;
  @ViewInject(R.id.a_park_no_checkin_compare_tv)
  private TextView tomorrowNumTv;
  @ViewInject(R.id.a_park_car_num_tv)
  private TextView carUsingNumTv;
  @ViewInject(R.id.a_park_car_num_compare_tv)
  private TextView carFreeNumTv;
  @ViewInject(R.id.a_park_caddie_num_tv)
  private TextView caddieWorkingTv;
  @ViewInject(R.id.a_park_caddie_compare_tv)
  private TextView caddieFreeNumTv;
  //list
  @ViewInject(R.id.a_park_listview)
  private ListView listView;
  @ViewInject(R.id.a_park_refresh_ll)
  private View refView;
  //
  private ErrorView errorView;
  //var
  private List<ParkBean.fairway> listData = new ArrayList<>();
  private MyBaseAdapter<ParkBean.fairway> adapter;
  //var
  private int inParkNum;
  private String inParkRatio;
  private int noCheckInNum;
  private int tomorrowNum;
  private int carUsingNum;
  private int carFreeNum;
  private int caddieWorkingNum;
  private int caddieFreeNum;

  private int groupNum;
  private int partitionNum;
  //
  private IFairwayPresenter worker;


  public interface IFairwayPresenter extends IPresenterImp {

    void getData();
  }

  public FairwayView(Activity act, IFairwayPresenter worker) {
    this.worker = worker;
    ViewUtils.autoInjectAllField(this, act);
    super.init(act);
  }

  @Override
  protected void setViews() {
    topTitle.setText(TextUtil.Text(worker.getActivityTitle()));
    errorView = new ErrorView(context);
    adapter = new MyBaseAdapter<ParkBean.fairway>(R.layout.item_park, context, listData) {
      @Override
      public View getView(int position, View view, ViewGroup parent, ParkBean.fairway fairway) {
        if (null == fairway) {
          return view;
        }
        setBackgroupdRes(R.id.item_park_oval_tv,
            EnumUtil.getParkStateRes(fairway.fairwayCondition));
        setText(R.id.item_park_state_tv, EnumUtil.getParkState(fairway.fairwayCondition));
        setText(R.id.item_park_hole_tv, fairway.holeNum);
        setText(R.id.item_park_group_tv, String.valueOf(fairway.groupNum));
        setBackgroupdColor(R.id.item_park_layout_ll,
            ContextCompat.getColor(context, (position % 2 == 0) ? R.color.bg_gray_white
                : R.color.bg));
        return view;
      }
    };
    listView.setAdapter(adapter);

  }

  @Override
  protected void setListener() {
    refView.setOnClickListener(v -> worker.getData());
    topBack.setOnClickListener(v -> worker.back());
  }

  @Override
  public void showProgress() {
    if (null != errorView) {
      errorView.showLoading(listView, true);
    }
  }

  @Override
  public void stopProgress() {
    AnimaUtil.stopLoading();
  }

  @Override
  public void getDataFail(String msg) {
    getDataFail(msg, -1);
  }

  @Override
  public void getDataFail(String msg, int code) {
    if (null != errorView) {
      errorView.showError(listView, msg, code, null);
    }
  }

  @Override
  public void getDataSuccess(Object obj) {
    ParkBean bean = (ParkBean) obj;
    if (null == bean || Util.listIsNull(bean.fairwayDataList)) {
      getDataFail(ErrorUtil.NODATA, ErrorUtil.NODATA_CODE);
      return;
    }
    showInfo(bean);
    if (null != errorView) {
      errorView.showData(listView);
    }
    listData.clear();
    listData.addAll(bean.fairwayDataList);
    adapter.updateUI();
  }

  private void showInfo(ParkBean m) {
    if (null != m.leaveData) {
      inParkNum = m.leaveData.leaveNum;
      groupNum = m.leaveData.groupNum;
      partitionNum = m.leaveData.partitionNum;
    }
    if (null != m.recordData) {
      noCheckInNum = m.recordData.recordNum;
      tomorrowNum = m.recordData.tomorrowNum;
    }
    if (null != m.carRecordData) {
      carUsingNum = m.carRecordData.usedNum;
      carFreeNum = m.carRecordData.freeNum;
    }
    if (null != m.caddieRecordData) {
      caddieWorkingNum = m.caddieRecordData.usedNum;
      caddieFreeNum = m.caddieRecordData.freeNum;
    }
    playingNumTv.setText(String.valueOf(inParkNum));

    if (partitionNum > 0) {
      inParkRatio = groupNum + "/" + partitionNum;
    }

    playingRatioTv.setText("占用率" + (Util.isNull(inParkRatio) ? "0" : inParkRatio));
    noCheckInNumTv.setText(String.valueOf(noCheckInNum));
    tomorrowNumTv.setText("明日" + String.valueOf(tomorrowNum) + "人");
    carUsingNumTv.setText(String.valueOf(carUsingNum));
    carFreeNumTv.setText("空闲" + String.valueOf(carFreeNum) + "辆");
    caddieWorkingTv.setText(String.valueOf(caddieWorkingNum));
    caddieFreeNumTv.setText("空闲" + String.valueOf(caddieFreeNum) + "人");
  }
}
